package com.tuanzi.loan.business.repository.pageTemplate.impl;

import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.repository.pageTemplate.custom.LoanPageTemplateInfoRepositoryCustom;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.pageTemplate.LoanPageTemplateInfoRequest;
import com.tuanzi.loan.vo.response.pageTemplate.LoanPageTemplateInfoResponse;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_PAGE_TEMPLATE_INFO;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_PAGE_TEMPLATE_PRODUCT;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;

/**
 * @Auther: hequnayu
 * @Date: 2019/9/16 19:20
 * @Description:
 */
public class LoanPageTemplateInfoRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanPageTemplateInfoRepositoryCustom {

    @Autowired
    private DictionaryService dictionaryService;

    @Override
    protected TableImpl getTable() {
        return LOAN_PAGE_TEMPLATE_INFO;
    }

    @Override
    public Page<LoanPageTemplateInfoResponse> list(LoanPageTemplateInfoRequest searchRequest, Pageable pageable) {
        fields(LOAN_PAGE_TEMPLATE_INFO.ID,LOAN_PAGE_TEMPLATE_INFO.NAME,LOAN_PAGE_TEMPLATE_INFO.STATUS,LOAN_PAGE_TEMPLATE_INFO.PARENT_ID,LOAN_PAGE_TEMPLATE_INFO.UPDATOR,LOAN_PAGE_TEMPLATE_INFO.UPDATE_TIME,LOAN_PAGE_TEMPLATE_INFO.CREATE_TIME,
                DSL.groupConcatDistinct(COMM_PRODUCT_PRDID.NAME).separator("，").as("products"));
        leftJoin(LOAN_PAGE_TEMPLATE_PRODUCT, LOAN_PAGE_TEMPLATE_INFO.ID.eq(LOAN_PAGE_TEMPLATE_PRODUCT.TEMPLATE_ID));
        leftJoin(COMM_PRODUCT_PRDID, COMM_PRODUCT_PRDID.PRDID.eq(LOAN_PAGE_TEMPLATE_PRODUCT.PRODUCT_ID));
        eq(LOAN_PAGE_TEMPLATE_INFO.ID, searchRequest.getId());
        contains(LOAN_PAGE_TEMPLATE_INFO.NAME, searchRequest.getName());
        eq(LOAN_PAGE_TEMPLATE_INFO.PARENT_ID, searchRequest.getParentId());
        eq(LOAN_PAGE_TEMPLATE_INFO.STATUS, searchRequest.getStatus());
        eq(LOAN_PAGE_TEMPLATE_PRODUCT.PRODUCT_ID, searchRequest.getProductId());
        groupBy(LOAN_PAGE_TEMPLATE_INFO.ID);
        return search(pageable, new LoanPageTemplateInfoSearchResponseRecordMapper());
    }

    private final class LoanPageTemplateInfoSearchResponseRecordMapper implements RecordMapper<Record, LoanPageTemplateInfoResponse> {

        @Override
        public LoanPageTemplateInfoResponse map(Record record) {
            LoanPageTemplateInfoResponse response = new LoanPageTemplateInfoResponse();
            Dictionary large_common_product = dictionaryService.findByCodeAndValue1("LARGE_COMMON_PRODUCT", record.get(LOAN_PAGE_TEMPLATE_INFO.PARENT_ID));
            if (large_common_product == null){
                response.setParentName("通用");
            }else{
                response.setParentName(large_common_product.getName());
            }
            response.setId(record.get(LOAN_PAGE_TEMPLATE_INFO.ID));
            response.setStatus(record.get(LOAN_PAGE_TEMPLATE_INFO.STATUS));
            response.setName(record.get(LOAN_PAGE_TEMPLATE_INFO.NAME));
            if (record.get("products") == null){
                response.setProducts("通用");
            }else{
                response.setProducts(record.get("products").toString());
            }
            response.setUpdator(record.get(LOAN_PAGE_TEMPLATE_INFO.UPDATOR));
            response.setUpdateTime(record.get(LOAN_PAGE_TEMPLATE_INFO.UPDATE_TIME));
            response.setCreateTime(record.get(LOAN_PAGE_TEMPLATE_INFO.CREATE_TIME));

            return response;
        }
    }
}
